﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.Windows;

namespace RecommendatioNetwork
{
    public class Channel : IDisposable
    {
        IContract channel = null;
        ChannelFactory<IContract> factory = null;

         /// <summary>
         /// Costruttore del canale, si occupa del settaggio dei valori di Binding
         /// </summary>
         /// <param name="add">Uri su cui viene costruito il canale</param>
         public Channel(Uri uri)
         {
             try
             {   
                 BasicHttpBinding bindingType = new BasicHttpBinding();
                 bindingType.OpenTimeout = new TimeSpan(0, 0, 5);
                 bindingType.CloseTimeout = new TimeSpan(0, 0, 5);
                 bindingType.ReceiveTimeout = new TimeSpan(0, 0, 5);
                 bindingType.SendTimeout = new TimeSpan(0, 0, 5);
                 factory = new ChannelFactory<IContract>(bindingType, new EndpointAddress(uri));
                 channel = factory.CreateChannel();
             }
             catch (Exception) { Console.WriteLine("TimeoutException"); }

        }

        /// <summary>
        /// Metodo che implementa l'inerfaccia IDispose, si occupa di chiudere i canali creati dalla channel Factory
        /// </summary>
        public void Dispose()
        {
            try
            {         
                factory.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Channels.cs" + e.Message);
            }
        }
        /// <summary>
        /// Proprietà che torna il canale
        /// </summary>
         public IContract SingleChannel
        {
            get{return channel;}
        }
    }
}
